'use strict';

angular.module('fbdApp.ruleModule')

.controller('rulesCtrl', function($scope, fbdService, $sce,
  modalService, $filter) {
  console.log($scope.testType);
  // $scope.articleList = [{
  //   location: 'a',
  //   text: 'a'
  // }, {
  //   location: 'b',
  //   text: 'b'
  // }];

  // var ruleFilter = $filter('testRuleFilter');
  // $scope.testa = "abc<br \>asdfsldfkj";
  // $scope.testa = $sce.trustAsHtml("abc<span class='highlight'>aaa</span>");
  $scope.modes = [{
    value: 1,
    content: '匹配开头'
  }, {
    value: 2,
    content: '匹配开头结尾'
  }, {
    value: 3,
    content: '匹配全文'
  }];
  // $scope.obj = JSON.parse($scope.testing);
  $scope.showArticle = function() {
    $scope.showRuleResult = false;
  }

  // $scope.articleList = shareData.articleList;
  console.log($scope.articleList, 'articles');

  // $scope.ruleObj = JSON.parse($scope.ruleData);
  $scope.ruleObj = {};
  // 修改时填充ruleObj
  if ($scope.ruleData != undefined) {
    $scope.ruleObj = $scope.ruleData;
    $scope.ruleObj.type = $scope.ruleObj.type.toString();
    $scope.ruleObj.limit = $scope.ruleObj.limit.toString();
    $scope.ruleObj.similar = $scope.ruleObj.similar.toString();
    // $scope.ruleObj = JSON.parse($scope.ruleData);
    console.log($scope.ruleData);
  }
  // 否则设置默认值
  else {
    $scope.ruleObj.limit = '2';
    $scope.ruleObj.similar = '0';
    $scope.ruleObj.type = '1';
  }

  // $scope.limitTemp = parseInt($scope.ruleObj.limit);
  // $scope.ruleObj.limit = $scope.limitTemp.toString();

  if ($scope.testType == 'file') {
    $scope.testObj = {
      type: 'file',
      // content: shareData.fileName
      content: $scope.fileName
    };
  } else {
    $scope.testObj = {
      type: 'string'
    };
  }

  // $scope.ruleData = {};
  // $scope.ruleData.text = {};
  // $scope.ruleData.rule = {};
  // $scope.ruleData.rule.type = "1";
  // $scope.$on('FileName', function(event, data) {
  //   $scope.ruleData.text.fileName = data;
  // });
  // $scope.fileName = shareData.fileName;
  // $scope.realFileName = shareData.realFileName;
  // $scope.ruleData.text.fileName = shareData.fileName;
  $scope.testRuleResult = {};
  $scope.addData = {};
  // $scope.addData = {};

  $scope.test = function() {
    // console.log(ruleData);
    $scope.showRuleResult = true;
    $scope.postData = $filter('testRuleFilter')($scope.ruleObj, $scope.testObj);
    // $scope.testRuleResult = [1, 2, 3];
    // console.log($scope.testRuleResult.length);
    var promise = fbdService.test(
      'http://localhost:3000/api/rules/detect',
      $scope.postData);
    promise.then(function(res) {
      // console.log(res);
      if (res.result.length) {
        $scope.isTested = true;
        // console.log($scope.ruleData.rule.type);
        // 结果处理
        $scope.testRuleResult.info = '共有' + res.result.length + '条记录';
        $scope.testRuleResult.data = res.result;
        if ($scope.testType != 'file') {
          $scope.testRuleResult.highlightText = $filter(
            'highlightFilter')(res.result, $scope.testObj.content);
        }
        $scope.addData = res.rule;
        // console.log($scope.addData);
        // var name = $scope.postData.name;
        // console.log($scope.ruleData);
        // 赋值给addData以便保存
        // $scope.addData = res.rule;
        // $scope.addData.name = name;

        // console.log($scope.addData);
        // console.log(res.rule);
        // alert(res + 'alert')
      }
      if (res.result.length == 0) {
        $scope.testRuleResult.info = '无任何结果，请重新修改规则';
        $scope.testRuleResult.data = [];
      }
      if (res.result.length == undefined) {
        $scope.testRuleResult.info = '请等待...';
        $scope.testRuleResult.data = [];
      }
    }, function(err) {
      $scope.testRuleResult.info = err + '，请重新发送请求';
      $scope.testRuleResult.data = [];
    });
  }

  $scope.save = function() {
    console.log($scope.ruleObj);
    $scope.addData.name = $scope.ruleObj.name;
    // console.log($scope.modify);

    if ($scope.modify == undefined) {
      var promise = fbdService.save(
        'http://localhost:3000/api/rules/add',
        $scope.addData);
    } else {
      $scope.addData._id = $scope.ruleObj._id;
      var promise = fbdService.save(
        'http://localhost:3000/api/rules/modify',
        $scope.addData);
    }
    promise.then(function(res) {
      // $scope.ok = function() {
      //   $scope.$close($scope, res);
      // };
      $scope.isSaved = true;
      $scope.$emit('isSaved', $scope.isSaved);
      $scope.saveInfo = '保存成功！';
      // delete res.__v;
      // delete res._id;
      $scope.savedRule = res;
      console.log(res);
      // $scope.modalInfo.buttonDisabled = false;

      // modalService.closeModal($scope, res);
      // $scope.$emit('articleList', $scope.testRuleResult.data);
      if ($scope.testType == 'file') {
        $scope.articleList = $scope.testRuleResult.data;
        // shareData.articleList = $scope.testRuleResult.data;
      }
      // shareData.rules.push($scope.addData);
      // console.log(shareData);
      // console.log($scope.testRuleResult.data, 'save');
    }, function(err) {
      console.log(err);
      $scope.isSaved = false;

      $scope.$emit('isSaved', $scope.isSaved);


      $scope.saveInfo = '保存失败！原因：' + err;

      // console.log(err);
    });

    // $scope.$watch($scope.savedRule, function(v) {
    //   modalService.closeModal($scope, v);
    // });

  };


});
